/**   
 * Copyright (c) 2013-3-14 by droidsde  
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT 
 * WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT 
 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
 * IN CONNECTION WITH THE SOFTWARE OR 
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @Title: UIHelper.java 
 * @version 
 */
package com.librariy.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class UIHelper {
    private static Dialog mDialog;
    private static Toast mToast;
    public static synchronized void showDialog(Context mContext, int resId) {
        String message = (mContext != null && resId > 0) ? mContext.getString(resId) : "正在加载数据";
        UIHelper.showDialog(mContext, message);        
    }
    public static synchronized void showDialog(Context mContext, String message) {
        dismissDialog();
        if (!(mContext instanceof Activity)){
            return;
        }
        ProgressDialog mProgressDialog = new ProgressDialog((Activity)mContext);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        //mProgressDialog.getWindow().setWindowAnimations(0);
        mProgressDialog.setMessage(TextUtils.isEmpty(message) ? "" : message);
        mProgressDialog.setCancelable(false);
        mDialog=mProgressDialog;
        mDialog.show();
    }

    public static synchronized void showDialog(Context mContext) {
        UIHelper.showDialog(mContext, "正在加载数据");
    }

    public static synchronized void dismissDialog() {
        if (mDialog != null&&mDialog.isShowing()){
            mDialog.dismiss();
        }
        mDialog=null;
    }
    public static void showAlertDialog(Context mContext,String text) {
        dismissDialog();
        if (!(mContext instanceof Activity)){
            return;
        }
        AlertDialog.Builder builder=new AlertDialog.Builder(mContext,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setTitle("提示");
        builder.setMessage(text);
        builder.setNegativeButton("关闭",null);
        mDialog=builder.create();
        mDialog.show();
    }
    public static synchronized void showToast(Context context, int resId) {
        UIHelper.showToast(context, context.getString(resId));
    }

    public static synchronized void showToast(Context context, String text) {
        UIHelper.showToast(context, text, Toast.LENGTH_SHORT);
    }

    public static synchronized void dismissToast() {
        if (mToast == null) return;
        mToast.cancel();
        mToast=null;
    }

    public static synchronized void showToast(Context context, String text, int type) {
        if (mToast != null)
            mToast.cancel();
        if (context == null)
            return;
        mToast = Toast.makeText(context, text, type);
        mToast.show();
    }
    public static void setStatusBarTransparent(Window window){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}
